// Copyright (C) 2011 - Tencent Inc.
// Author: Liu Shaohui (struggleliu@tencent.com)
//
// Created: 08/16/2011 09:29:54
// Description:

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>

#include "thirdparty/gflags/gflags.h"
#include "thirdparty/glog/logging.h"
#include "thirdparty/google/profiler.h"
#include "thirdparty/google/heap-profiler.h"

typedef unsigned int      uint32_t;

DEFINE_string(prof_output_path, "./prof.prof", "The profile output path.");
DEFINE_string(data_file, "./data.in", "The input data file");

#define BUFSIZE 1<<12
char buf[BUFSIZE];

int main(int argc, char** argv) {
    //ProfilerStart(FLAGS_prof_output_path.c_str());
    HeapProfilerStart("./heap-");
    uint32_t altitude = 0, read_len;
    uint32_t sum = 0;
    FILE * pFile = fopen(FLAGS_data_file.c_str(), "r");
    CHECK_NOTNULL(pFile);
    while ((read_len = fread(buf, sizeof(char), BUFSIZE, pFile)) > 0) {
        for (uint32_t i = 0;  i < read_len; i ++) {
            if (buf[i] != '\n') {
                altitude = altitude * 10 + buf[i] - '0';
            } else {
                sum += altitude;
                altitude = 0;
            }
        }
    }
    printf("%u\n", sum);
    fclose(pFile);
    //ProfilerStop();
    HeapProfilerStop();
    return 0;
}
